const tool = require('../tools/tool')

const exp = {
    login: {
        exp: '用户登录',
        method: 'post',
        async func(data) {
            let account = data.param.account
            let password = data.param.password
            let userData = await tool.methods.user().get.byAccount({ account, withDeleted: false, withFakeData: false })
            if (userData === undefined) throw '用户不存在'
            if (password != userData.password) throw '密码错误'
            let newToken = tool.genUserToken({ account })
            await tool.methods.user().setToken.byAccount(account, newToken)
            let re = await tool.methods.user().get.byAccount({ account, withDeleted: false, withFakeData: false })
            delete re.password
            return re 
        }
    },
    
}

module.exports = exp